<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>adaboost.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>model&nbsp;</span>=&nbsp;<span class=defun_name>adaboost</span>(<span class=defun_in>data,options</span>)<br>
<span class=h1>%&nbsp;ADABOOST&nbsp;AdaBoost&nbsp;algorithm.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;adaboost(data,options)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;This&nbsp;function&nbsp;implements&nbsp;the&nbsp;AdaBoost&nbsp;algorithm&nbsp;which</span><br>
<span class=help>%&nbsp;&nbsp;produces&nbsp;a&nbsp;classifier&nbsp;composed&nbsp;from&nbsp;a&nbsp;set&nbsp;of&nbsp;weak&nbsp;rules.</span><br>
<span class=help>%&nbsp;&nbsp;The&nbsp;weak&nbsp;rules&nbsp;are&nbsp;learned&nbsp;by&nbsp;a&nbsp;weak&nbsp;learner&nbsp;which&nbsp;is</span><br>
<span class=help>%&nbsp;&nbsp;specified&nbsp;in&nbsp;options.learner.&nbsp;The&nbsp;task&nbsp;of&nbsp;the&nbsp;weak&nbsp;learner</span><br>
<span class=help>%&nbsp;&nbsp;is&nbsp;to&nbsp;produce&nbsp;a&nbsp;rule&nbsp;with&nbsp;weighted&nbsp;error&nbsp;less&nbsp;then&nbsp;0.5.</span><br>
<span class=help>%&nbsp;&nbsp;The&nbsp;Adaboost&nbsp;algorithm&nbsp;calls&nbsp;in&nbsp;each&nbsp;stage&nbsp;the&nbsp;weak&nbsp;learner</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rule{t}&nbsp;=&nbsp;feval(options.learner,weight_data)</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;where&nbsp;the&nbsp;structure&nbsp;weight_data&nbsp;contains</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Training&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Labels&nbsp;of&nbsp;training&nbsp;vectos&nbsp;(1&nbsp;or&nbsp;2).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;.D&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Distribution&nbsp;(weights)&nbsp;over&nbsp;training&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;which&nbsp;defines&nbsp;the&nbsp;weighted&nbsp;error.</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;The&nbsp;item&nbsp;rule{t}.fun&nbsp;must&nbsp;contain&nbsp;name&nbsp;of&nbsp;function</span><br>
<span class=help>%&nbsp;&nbsp;which&nbsp;classifies&nbsp;vector&nbsp;X&nbsp;by&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;y&nbsp;=&nbsp;feval(&nbsp;rule{t}.fun,&nbsp;X,&nbsp;rule{t}).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;It&nbsp;is&nbsp;assumed&nbsp;that&nbsp;the&nbsp;weak&nbsp;rule&nbsp;responds&nbsp;with&nbsp;labels&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;1&nbsp;or&nbsp;2&nbsp;(not&nbsp;1,-1&nbsp;as&nbsp;used&nbsp;in&nbsp;AdaBoost&nbsp;literature).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;[struct]&nbsp;Input&nbsp;training&nbsp;data:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Training&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Labels&nbsp;of&nbsp;training&nbsp;vectos&nbsp;(1&nbsp;or&nbsp;2).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;[struct]&nbsp;Parameters&nbsp;of&nbsp;the&nbsp;AdaBoost:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.learner&nbsp;[string]&nbsp;Name&nbsp;of&nbsp;the&nbsp;weak&nbsp;learner.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.max_rules&nbsp;[1x1]&nbsp;Maximal&nbsp;number&nbsp;of&nbsp;weak&nbsp;rules&nbsp;(defaul&nbsp;100).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;paramater&nbsp;defines&nbsp;a&nbsp;stopping&nbsp;condition.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.err_bound&nbsp;[1x1]&nbsp;AdaBoost&nbsp;stops&nbsp;if&nbsp;the&nbsp;upper&nbsp;bound&nbsp;on&nbsp;the&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;empirical&nbsp;error&nbsp;drops&nbsp;below&nbsp;the&nbsp;err_bound&nbsp;(default&nbsp;0.001).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.learner_options&nbsp;Additinal&nbsp;options&nbsp;used&nbsp;when&nbsp;the&nbsp;weak&nbsp;learner</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;is&nbsp;called.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.verb&nbsp;[1x1]&nbsp;If&nbsp;1&nbsp;then&nbsp;some&nbsp;info&nbsp;is&nbsp;displayed.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;AdaBoost&nbsp;classifier:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.rule&nbsp;[cell&nbsp;1&nbsp;x&nbsp;T]&nbsp;Weak&nbsp;classification&nbsp;rules.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Alpha&nbsp;[1&nbsp;x&nbsp;T]&nbsp;Weights&nbsp;of&nbsp;the&nbsp;rules.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.WeightedErr&nbsp;[1&nbsp;x&nbsp;T]&nbsp;Weighted&nbsp;errors&nbsp;of&nbsp;the&nbsp;weak&nbsp;rules.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Z&nbsp;[1&nbsp;x&nbsp;T]&nbsp;Normalization&nbsp;constants&nbsp;of&nbsp;the&nbsp;distribution&nbsp;D.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.ErrBound&nbsp;[1&nbsp;x&nbsp;T]&nbsp;Upper&nbsp;bounds&nbsp;on&nbsp;the&nbsp;empirical&nbsp;error.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;load('riply_trn');</span><br>
<span class=help>%&nbsp;&nbsp;options.learner&nbsp;=&nbsp;'weaklearner';</span><br>
<span class=help>%&nbsp;&nbsp;options.max_rules&nbsp;=&nbsp;100;</span><br>
<span class=help>%&nbsp;&nbsp;options.verb&nbsp;=&nbsp;1;</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;adaboost(data,options);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;ppatterns(data);&nbsp;pboundary(model);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;hold&nbsp;on;&nbsp;plot(model.ErrBound,'r');&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;plot(model.WeightedErr);</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also:&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;ADACLASS,&nbsp;WEAKLEARNER.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2004,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;11-aug-2004,&nbsp;VF</span><br>
<br>
<hr>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;2,&nbsp;options&nbsp;=&nbsp;[];&nbsp;<span class=keyword>else</span>&nbsp;options&nbsp;=&nbsp;c2s(options);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'max_rules'</span>),&nbsp;options.max_rules&nbsp;=&nbsp;100;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'err_bound'</span>),&nbsp;options.err_bound&nbsp;=&nbsp;0.001;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'learner'</span>),&nbsp;options.learner&nbsp;=&nbsp;<span class=quotes>'weaklearner'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'learner_options'</span>),&nbsp;options.learner_options&nbsp;=&nbsp;[];&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'verb'</span>),&nbsp;options.verb&nbsp;=&nbsp;0;&nbsp;<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;take&nbsp;data&nbsp;dimensions</span><br>
[dim,num_data]&nbsp;=&nbsp;size(data.X);<br>
<br>
<span class=comment>%&nbsp;initial&nbsp;distribution&nbsp;over&nbsp;training&nbsp;samples</span><br>
data.D&nbsp;=&nbsp;ones(num_data,1)/num_data;<br>
<br>
model.Alpha&nbsp;=[];<br>
model.Z&nbsp;=&nbsp;[];<br>
model.WeightedErr&nbsp;=&nbsp;[];<br>
model.ErrBound&nbsp;=&nbsp;[];<br>
<br>
t&nbsp;=&nbsp;0;<br>
go&nbsp;=&nbsp;1;<br>
<span class=keyword>while</span>&nbsp;go,<br>
&nbsp;&nbsp;t&nbsp;=&nbsp;t&nbsp;+&nbsp;1;<br>
<br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;options.verb,&nbsp;<span class=io>fprintf</span>(<span class=quotes>'rule&nbsp;%d:&nbsp;'</span>,&nbsp;t);&nbsp;<span class=keyword>end</span><br>
<br>
&nbsp;&nbsp;<span class=comment>%&nbsp;call&nbsp;weak&nbsp;learner</span><br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;~isempty(options.learner_options),<br>
&nbsp;&nbsp;&nbsp;&nbsp;rule&nbsp;=&nbsp;<span class=eval>feval</span>(options.learner,data,options.learner_options);<br>
&nbsp;&nbsp;<span class=keyword>else</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;rule&nbsp;=&nbsp;<span class=eval>feval</span>(options.learner,data);<br>
&nbsp;&nbsp;<span class=keyword>end</span>&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;y&nbsp;=&nbsp;<span class=eval>feval</span>(rule.fun,data.X,rule);<br>
<br>
&nbsp;&nbsp;werr&nbsp;=&nbsp;(y(:)~=data.y(:))'*data.D(:);<br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;options.verb,&nbsp;<span class=io>fprintf</span>(<span class=quotes>'werr=%f'</span>,&nbsp;werr);&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;werr&nbsp;&lt;&nbsp;0.5,<br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;alpha&nbsp;=&nbsp;0.5*log((1-werr)/werr);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;yh(i)&nbsp;=&nbsp;+1&nbsp;for&nbsp;data.y(i)&nbsp;==&nbsp;y(i)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;yh(i)&nbsp;=&nbsp;-1&nbsp;for&nbsp;data.y(i)&nbsp;~=&nbsp;y(i)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;yh&nbsp;=&nbsp;2*(y(:)&nbsp;==&nbsp;data.y(:))-1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;weights&nbsp;=&nbsp;data.D.*exp(-alpha*yh(:));<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;normalization&nbsp;constant</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;Z&nbsp;=&nbsp;sum(weights);<br>
&nbsp;&nbsp;&nbsp;&nbsp;data.D&nbsp;=&nbsp;weights/Z;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;upper&nbsp;bound&nbsp;on&nbsp;the&nbsp;training&nbsp;error</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;err_bound&nbsp;=&nbsp;prod(model.Z);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;store&nbsp;variables</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;model.Z&nbsp;=&nbsp;[model.Z;&nbsp;Z];<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.Alpha&nbsp;=&nbsp;[model.Alpha;alpha];<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.rule{t}&nbsp;=&nbsp;rule;<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.ErrBound&nbsp;=&nbsp;[model.ErrBound;&nbsp;err_bound];<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;stopping&nbsp;conditions</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;t&nbsp;&gt;=&nbsp;options.max_rules,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;=&nbsp;0;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.exitflag&nbsp;=&nbsp;1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>elseif</span>&nbsp;err_bound&nbsp;&lt;=&nbsp;options.err_bound,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;=&nbsp;0;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.exitflag&nbsp;=&nbsp;2;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;options.verb,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>',&nbsp;alpha=%f,&nbsp;err_bound=%f\n'</span>,alpha,err_bound);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;<span class=keyword>else</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;the&nbsp;weighted&nbsp;error&nbsp;is&nbsp;over&nbsp;0.5</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;options.verb,&nbsp;<span class=io>fprintf</span>(<span class=quotes>'&gt;=&nbsp;0.5&nbsp;thus&nbsp;stop.\n'</span>);&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;go&nbsp;=&nbsp;0;<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.exitflag&nbsp;=&nbsp;0;<br>
&nbsp;&nbsp;<span class=keyword>end</span><br>
<br>
&nbsp;&nbsp;model.WeightedErr&nbsp;=&nbsp;[model.WeightedErr;&nbsp;werr];<br>
<br>
<span class=keyword>end</span><br>
<br>
model.fun&nbsp;=&nbsp;<span class=quotes>'adaclass'</span>;<br>
<br>
<span class=jump>return</span>;<br>
<span class=comment>%&nbsp;EOF</span><br>
</code>
